//package com.example.recordingpen_datacollection.service.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.example.recordingpen_datacollection.Utils.AIUtil.AIChangeUtil;
//import com.example.recordingpen_datacollection.Utils.AIUtil.pojo.aliyun.AliYunJsonResult;
//import com.example.recordingpen_datacollection.Utils.http.AliYunHttpUtil;
//import com.example.recordingpen_datacollection.Utils.http.BDokHttpUtil;
//import com.example.recordingpen_datacollection.Utils.http.HuoSanHttpUtil;
//import com.example.recordingpen_datacollection.Utils.http.Sign2;
//import com.example.recordingpen_datacollection.Utils.http.pojoUtil.zj.ZJJsonPutRoot;
//import com.example.recordingpen_datacollection.Utils.http.pojoUtil.zj.ZJJsonRootBean;
//import com.example.recordingpen_datacollection.Utils.hy.AIUtil;
//import com.example.recordingpen_datacollection.Utils.hy.HyResponse;
//import com.example.recordingpen_datacollection.entity.Businessdata;
//import com.example.recordingpen_datacollection.entity.DosageStatistics;
//import com.example.recordingpen_datacollection.entity.Vo.AISumUp;
//import com.example.recordingpen_datacollection.entity.Vo.AIZFYVo;
//import com.example.recordingpen_datacollection.redisService.RedisService;
//import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
//import com.example.recordingpen_datacollection.service.AIService;
//import com.example.recordingpen_datacollection.service.BusinessdataService;
//import com.example.recordingpen_datacollection.service.DosageStatisticsService;
//import com.example.recordingpen_datacollection.xunfeigpt.bean.ResultBean;
//import com.example.recordingpen_datacollection.xunfeigpt.service.PushService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.io.IOException;
//
//import static com.example.recordingpen_datacollection.Utils.http.HttpUrl.*;
//import static com.example.recordingpen_datacollection.config.constant.ConstantName.BD_ACCESS_TOKEN;
//import static com.example.recordingpen_datacollection.config.constant.ConstantName.ZJ_ACCESS_TOKEN;
//
//@Service
//public class AIServiceImplCP implements AIService {           //ADMIN_MESSAGE_TOKEN_e46366fd-befe-45bd-95a5-88fa8c883dfc
//    @Autowired
//    private BusinessdataService businessdataService;
//    @Autowired
//    private DosageStatisticsService dosageStatisticsService;
//    @Autowired
//    private RedisService redisService;
//    @Autowired
//    private PushService pushService;
//    @Override
//    @Transactional
//    public String txtSumUp(AISumUp aiSumUp,Businessdata businessdataOne) {
//
//
//
//
//        String txt = aiSumUp.getContent();
//        Businessdata bus = new Businessdata();
//        bus.setId(businessdataOne.getId());
//        bus.setDosage(businessdataOne.getDosage()-txt.length());
//        boolean update = businessdataService.updateById(bus);
//        if(!update){
//            throw new JWException(500,"操作失败");
//        }
//        DosageStatistics dosageStatistics= new DosageStatistics();
//        dosageStatistics.setLoginId(businessdataOne.getLoginId());
//        dosageStatistics.setBusinessId(aiSumUp.getServerType());
//        dosageStatistics.setTextName(aiSumUp.getTextName());
//        dosageStatistics.setUsageFrequency((long) txt.length());
//
//        boolean save = dosageStatisticsService.save(dosageStatistics);
//        if(!save){
//            throw new JWException(500,"记录失败");
//        }
//
//
////调用总结接口
//        String txtSumUp = "";
//        HyResponse hyresponse = null;
//        if(aiSumUp.getServerType() ==2){            //腾讯大模型
//            System.out.println(2);
//            String action = "ChatStd";
//            switch (aiSumUp.getAction()){
//                case 0:
//                    action= "ChatStd";
//                    break;
//                case 1:
//                    action= "ChatPro";
//                    break;
//                default:
//                    action= "ChatStd";
//                    break;
//            }
//
//
//
//                txtSumUp = AIUtil.staAianswer("user", aiSumUp.getContent(),action);
//
//            System.out.println(hyresponse  +"hyresponse");
//
//        }else if(aiSumUp.getServerType() ==3) {    //讯飞大模型
//            System.out.println(3);
//
//            ResultBean resultBean = pushService.pushMessageToXFServer(String.valueOf(aiSumUp.getLoginId()), aiSumUp.getContent());
////            if(!resultBean.getErrorCode().equals("200")){
////                throw new JWException(Integer.parseInt(resultBean.getErrorCode()),resultBean.getMessage());
////            }
//            txtSumUp = (String) resultBean.getData();
//        } else if(aiSumUp.getServerType() ==4  && false) {
//            System.out.println(4);
//            boolean bd_access_token = redisService.getKeyData(BD_ACCESS_TOKEN);
//            String access_token = null;
//            if(!bd_access_token){
//                try {
//                    String bDtokenData = BDokHttpUtil.getBDtoken();
//                    JSONObject jsonObject = JSON.parseObject(bDtokenData);
//
//                    // 从JSONObject中提取值
//                    access_token = jsonObject.getString("access_token");
//                    int expires_in = jsonObject.getIntValue("expires_in")-(100*1000);
//                    redisService.setTokenValue2(BD_ACCESS_TOKEN,expires_in,access_token);
//                    System.out.println("重新调用token");
//                } catch (IOException e) {
//                    throw new JWException(500,"获取DB_token异常");
//                }
//            } else {
//                access_token = (String)redisService.getValue(BD_ACCESS_TOKEN);
//                System.out.println("没有调用token");
//            }
//            try {
//                String  getBDData = BDokHttpUtil.getBDData(BDurlData,access_token, "user", aiSumUp.getContent());
//                JSONObject jsonObject = JSON.parseObject(getBDData);
//                String error_code = jsonObject.getString("error_code");
//                if(error_code != null){
//                    throw new JWException(500,getBDData);
//                }
//                // 从JSONObject中提取值
//                txtSumUp = jsonObject.getString("result");
//            } catch (IOException e) {
//                throw new JWException(500,"访问DB异常");
//            }
//        } else if(aiSumUp.getServerType() == 5 || aiSumUp.getServerType() ==4){
//            System.out.println(5);
//            boolean bd_access_token = redisService.getKeyData(BD_ACCESS_TOKEN);
//            String access_token = null;
//            if(!bd_access_token){
//                try {
//                    String bDtokenData = BDokHttpUtil.getBDtoken();
//                    JSONObject jsonObject = JSON.parseObject(bDtokenData);
//
//                    // 从JSONObject中提取值
//                    access_token = jsonObject.getString("access_token");
//                    int expires_in = jsonObject.getIntValue("expires_in")-(100*1000);
//                    redisService.setTokenValue2(BD_ACCESS_TOKEN,expires_in,access_token);
//                    System.out.println("重新调用token");
//                } catch (IOException e) {
//                    throw new JWException(500,"获取DB_token异常");
//                }
//            } else {
//                access_token = (String)redisService.getValue(BD_ACCESS_TOKEN);
//                System.out.println("没有调用token");
//            }
//            try {
//                String  getBDData = BDokHttpUtil.getBDData(BDurl128KData,access_token, "user", aiSumUp.getContent());
//                JSONObject jsonObject = JSON.parseObject(getBDData);
//                String error_code = jsonObject.getString("error_code");
//                if(error_code != null){
//                    throw new JWException(500,getBDData);
//                }
//                // 从JSONObject中提取值
//                String BDresult = jsonObject.getString("result");
//                txtSumUp = BDresult;
////                txtSumUp = BDHtmlLayoutUtil.convertToHtml(BDresult);
////                txtSumUp = BDHtmlLayoutV2Util.convertToHtml(BDresult);
//            } catch (IOException e) {
//                throw new JWException(500,"访问DB异常");
//            }
//        } else if(aiSumUp.getServerType() == 6){   //阿里大模型
//            System.out.println(6);
//            try {
////                AliYunJsonResult aliYunJsonResult = AliYunHttpUtil.getAliYunData("user", aiSumUp.getContent());
//                String aliYunJsonStr = AliYunHttpUtil.getOKHttpAliYunData("user", aiSumUp.getContent());
//                String substring = aliYunJsonStr.substring(0, 5);
//                if(substring.equals("请求错误:")){
//                    throw new JWException(400,aliYunJsonStr);
//                }
//                AliYunJsonResult aliYunJsonResult = JSON.parseObject(aliYunJsonStr, AliYunJsonResult.class);
//                txtSumUp = aliYunJsonResult.getOutput().getChoices().get(0).getMessage().getContent();
//            } catch (IOException e) {
//                throw new JWException(500,"访问aliyun异常");
//            }
//        } else if(aiSumUp.getServerType() == 7) {   //字节  火山、
//            System.out.println(7);
//            boolean zj_access_token = redisService.getKeyData(ZJ_ACCESS_TOKEN);
//            String access_token = null;
//            if(!zj_access_token){
//                String bDtokenData = null;
//                try {
//                    bDtokenData = Sign2.getApikey();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                ZJJsonRootBean zjJsonRootBean = JSON.parseObject(bDtokenData, ZJJsonRootBean.class);
//                String apiKey = zjJsonRootBean.getResult().getApiKey();
//                access_token = apiKey;
//                long expires_in = (zjJsonRootBean.getResult().getExpiredTime()-(120*1000))/1000;    //秒
//                redisService.setTokenValue2(ZJ_ACCESS_TOKEN,expires_in,access_token);
//            } else {
//                access_token = (String)redisService.getValue(ZJ_ACCESS_TOKEN);
//                System.out.println("没有调用token");
//            }
//            try {
//                String getZJData = HuoSanHttpUtil.getZJData(HuoSanUrlData, access_token, "user", aiSumUp.getContent());
//                JSONObject jsonObject = JSON.parseObject(getZJData);
//                String error_code = jsonObject.getString("error_code");
//                if(error_code != null){
//                    throw new JWException(500,getZJData);
//                }
//                ZJJsonPutRoot zjJsonPutRoot = JSON.parseObject(getZJData, ZJJsonPutRoot.class);
//                txtSumUp = zjJsonPutRoot.getChoices().get(0).getMessage().getContent();
//            } catch (IOException e) {
//                throw new JWException(500,"访问字节 火山模型异常");
//            }
//
//        }
//
//        if(txtSumUp == null){
//            throw new JWException(500,"获取消息为null");
//        }
//
//        //存储结果集
//        DosageStatistics dosageResult = new DosageStatistics();
//        dosageResult.setId(dosageStatistics.getId());
//        dosageResult.setFinalResult(txtSumUp);
//        dosageStatisticsService.updateById(dosageResult);
//        return txtSumUp;
//    }
//
//    @Override
//    public String AITxtUp(AIZFYVo aiSumUp ) {
//        //调用总结接口
//        String txtSumUp = "";
//        boolean bd_access_token = redisService.getKeyData(BD_ACCESS_TOKEN);
//        String access_token = null;
//        if(!bd_access_token){
//            try {
//                String bDtokenData = BDokHttpUtil.getBDtoken();
//                JSONObject jsonObject = JSON.parseObject(bDtokenData);
//
//                // 从JSONObject中提取值
//                access_token = jsonObject.getString("access_token");
//                int expires_in = jsonObject.getIntValue("expires_in")-(100*1000);
//                redisService.setTokenValue2(BD_ACCESS_TOKEN,expires_in,access_token);
//                System.out.println("重新调用token");
//            } catch (IOException e) {
//                throw new JWException(500,"获取DB_token异常");
//            }
//        } else {
//            access_token = (String)redisService.getValue(BD_ACCESS_TOKEN);
//            System.out.println("没有调用token");
//        }
//        try {
//            String  getBDData = BDokHttpUtil.getBDDataSet(access_token, "user", aiSumUp.getContent(),"Philips的AI助手");
//            JSONObject jsonObject = JSON.parseObject(getBDData);
//
//            // 从JSONObject中提取值
//             txtSumUp = jsonObject.getString("result");
//        } catch (IOException e) {
//            throw new JWException(500,"访问DB异常");
//        }
//        if(txtSumUp == null){
//            throw new JWException(500,"获取消息为null");
//        }
//
//
//        String replaceStr = AIChangeUtil.replaceStr(txtSumUp);
//
//
//
//        return replaceStr;
//    }
//
//
//}
